import java.io.*;
import java.net.*;


/** The main class of the server*/
public class Server {
    /** Server's Socket.*/
    private ServerSocket SerSoc;
    /** Table */
    private ManagerOfClients table;
    
    private int port;
	/**Servers constructor.
	 * @param port, port's number
	 * @param startCoins - start Coins
	 * @param smallBlind - smallBlind
	 * @param bigBlind - bigBlind
	 * @throws ServerException, if socket can't listen on the given port
	 */
	public Server(int port, int startCoins, int smallBlind, int bigBlind) throws ServerException
	{	
		this.port = port;
		try{
            this.SerSoc = new ServerSocket(port);
		}
		catch(IOException e)
		{
			throw new ServerException("port's error");
		}
		this.table = new ManagerOfClients(startCoins, smallBlind, bigBlind, port);
	}
	/**Method listens on the port */
    private void listen()
    {
         while(true)
         {
        	 Socket client = null;
        	 try
        	 {
        		 client = SerSoc.accept();
        		 System.out.println("newclient");
        	 }
        	 catch(IOException e)
        	 {
        		 System.err.println("connection is rejcected");
        	 }
        	 this.table.addNewClient(client);
         }
    }
	/** The main class of the server
	 * @param args[0] - port's number
	 * @param args[1] - start Coins
	 * @param args[2] - smallBlind
	 * @param args[3] - bigBlind
	 */
	public static void main(String[] args) {
        
		int port = 0;
		int startCoins = 0;
		int smallBlind = 0;
		int bigBlind = 0;
		try{
		     port = Integer.parseInt(args[0]);
		     startCoins = Integer.parseInt(args[1]);
		     bigBlind = Integer.parseInt(args[2]);
		     smallBlind = Integer.parseInt(args[3]);
		}
		catch(NumberFormatException e)
		{
			System.err.println("Inalid entrance arguments");
		}
		if( startCoins <= bigBlind || bigBlind <= smallBlind || smallBlind<=0)
		{
			System.err.println("Invalid value startCoins or bigBlind or SmallBlind");
		}
		else
		{
			Server server = null;
			try{
				server = new Server(port, startCoins, smallBlind, bigBlind);
			}
			catch(ServerException e)
			{
				System.err.println(e.getMessage());
			}	
			if(server != null)
			{
				server.listen();
			}
		}

	}
}
